package com.qyhl.webtv.module_news.utils.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.qyhl.cloud.webtv.module_news.R;


/**
 * Created by Administrator on 2018/4/17.
 */

public class FontSizeSeekBarView extends View {
    private final int smallRadius = 12;
    private final int bigRadius = 23;

    private int nowX = bigRadius;
    private Context context;
    private Paint paint;
    private int smallNum = 5;
    private int width;
    private int height;
    private int pos = 0;
    private int circleDis = 1;
    private int smallcolor;
    private int bigolor;
    private FontSizeSeekBarChangeListener fontSizeSeekBarChangeListener;

    public FontSizeSeekBarView(Context context) {
        this(context, null);
    }

    public FontSizeSeekBarView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public FontSizeSeekBarView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        initview();
    }

    private void initview() {
        paint = new Paint();
        paint.setStrokeWidth(2);
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.FILL);

        smallcolor = ContextCompat.getColor(context, R.color.global_gray_lv2);
        bigolor = ContextCompat.getColor(context, R.color.global_base);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);

        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        if (widthMode == MeasureSpec.EXACTLY) {
            width = widthSize;
        } else {
            width = 400;
        }
        if (heightMode == MeasureSpec.EXACTLY) {
            height = heightSize;
        } else {
            height = 80;
        }
        circleDis = (width - bigRadius * 2) / (smallNum - 1);
        //使初始设置位生效
        nowX = circleDis * pos + bigRadius;

        setMeasuredDimension(width, height);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                setNowX((int) event.getX());
                break;
            case MotionEvent.ACTION_MOVE:
                setNowX((int) event.getX());
                break;
            case MotionEvent.ACTION_UP:
                resetNowX((int) event.getX());
                break;
            default:
        }
        invalidate();
        return true;
    }

    private void setNowX(int x) {
        if (x < bigRadius) {
            nowX = bigRadius;
        } else if (x > width - bigRadius) {
            nowX = width - bigRadius;
        } else {
            nowX = x;
        }
    }

    private void resetNowX(int x) {
        //段号
        int posafter = x / circleDis;
        pos = (x - posafter * circleDis) < (circleDis / 2) ? posafter : posafter + 1;
        nowX = circleDis * pos + bigRadius;
        if (fontSizeSeekBarChangeListener != null) {
            fontSizeSeekBarChangeListener.onChangeListener(pos);
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        paint.setColor(smallcolor);
        //画背景线
        canvas.drawLine(bigRadius, height / 2,
                width - bigRadius, height / 2, paint);

        //绘制小圆
        for (int i = 0; i < smallNum; i++) {
            canvas.drawCircle(circleDis * i + bigRadius, height / 2, smallRadius, paint);
        }

        paint.setColor(bigolor);
        //绘制大圆
        canvas.drawCircle(nowX, height / 2, bigRadius, paint);
    }

    public void setSmallNum(int smallNum) {
        //至少两个点
        this.smallNum = Math.max(2, smallNum);
    }

    public int getPosition() {
        return pos;
    }

    public void setPosition(int position) {
        pos = position;
        nowX = circleDis * pos + bigRadius;
        invalidate();
    }

    public void setFontSizeSeekBarChangeListener(FontSizeSeekBarChangeListener fontSizeSeekBarChangeListener) {
        this.fontSizeSeekBarChangeListener = fontSizeSeekBarChangeListener;
    }

    public interface FontSizeSeekBarChangeListener {
        void onChangeListener(int pos);
    }
}
